package com.example.management;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.management.api.ApiRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ListPageLikeActivity extends AppCompatActivity {

    @BindView(R.id.page)
    EditText mPage;

    @BindView(R.id.size)
    EditText mSize;

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private RecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_page_like);
        ButterKnife.bind(this);

        mAdapter = new RecyclerAdapter(this,new ArrayList<ListLikeResult.DataBean>());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
    }


    @OnClick({R.id.query, R.id.clear_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.query:
                if (mPage.getText().toString().trim().isEmpty() || mSize.getText().toString().trim().isEmpty()) {
                    Toast.makeText(ListPageLikeActivity.this, "请确认输入是否完整!!!", Toast.LENGTH_SHORT).show();
                    return;
                }
                ApiRequest.getInstance()
                        .getListPageLike(Integer.valueOf(mPage.getText().toString().trim()), Integer.valueOf(mSize.getText().toString().trim()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new Subscriber<Object>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                Log.d("RAW", e.getMessage());
                            }

                            @Override
                            public void onNext(Object result) {
                                try {
                                    JSONObject json = new JSONObject(result.toString());
                                    int code = json.optInt("code");
                                    Log.d("RAW", "点击查询， 服务器返回 code ---> " + code);
                                    Toast.makeText(ListPageLikeActivity.this, "服务器返回 --->" + code, Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                break;
            case R.id.clear_text:
                if (!mPage.getText().toString().trim().isEmpty()) {
                    mPage.setText("");
                }
                if (!mSize.getText().toString().trim().isEmpty()) {
                    mSize.setText("");
                }
                break;
        }
    }
}
